package com.xysd.express.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.xysd.express.R;
import com.xysd.express.activity.login.LoginActivity;
import com.xysd.express.base.BaseActivity;
import com.xysd.express.util.ActivityTaskManeger;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

@ContentView(R.layout.activity_auto_web)
public class AutoWebActivity extends BaseActivity implements View.OnClickListener {

    @ViewInject(R.id.auto_wb)
    private WebView webView;
    private String url;
    private String loadUrl;
    private String title;

    @SuppressLint({ "JavascriptInterface", "SetJavaScriptEnabled" })
    @Override
    protected void init() {
        setTitleBar(getIntent().getStringExtra("title"));
        backImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setAppCacheEnabled(false);
        settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        settings.setAllowFileAccess(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        settings.setDatabaseEnabled(true);            // 开启database storage API功能
        settings.setDomStorageEnabled(true);          // 使用localstorage
        settings.setDefaultTextEncodingName("UTF-8"); // 设置默认编码
        //设置本地调用对象及其接口
//        webView.addJavascriptInterface(new JSholder(mActivity),"bairichang");

        Intent intent = getIntent();
        url = intent.getStringExtra("url");
        loadUrl = url;
//        accredit(loadUrl);//授权
        webView.loadUrl(loadUrl);
        Log.i("loadUrl",loadUrl);
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                Log.i("shouldOverrideUrlLoaUrl",url);
                loadUrl = url;
                if (url.contains("/Login/Entrance")){
                    startAty(LoginActivity.class);
                }else {
                    view.loadUrl(loadUrl);
                }
                return true;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                setTitleBar(view.getTitle());
            }
        });
        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                setTitleBar(title);
                Log.i("title", "title = " + title);
            }
        });

    }

    //授权
//    private void accredit(String url) {
//        Log.i("url",url);
//        if (!User.getInstance().isLogin){
//            return;
//        }
//
//        String Authkey_encode="";
//        String url_encode="";
//        try {
//            Authkey_encode= URLEncoder.encode(User.getInstance().AuthKey, "utf-8");
//            url_encode= URLEncoder.encode(url, "utf-8");
//        } catch (UnsupportedEncodingException e) {
//            e.printStackTrace();
//            Log.e("Exception",e.toString());
//        }
//        loadUrl = ConstantParser.HTTP_URI
//                + "/m-Android/Login/Auth?key="+ Authkey_encode
//                + "&url=" + url_encode
//                + "&timestamp="+ User.getInstance().Timestamp
//                + "&uid=" + User.getInstance().UserId;
//    }

    @Override
    public void onClick(View view) {
        if (webView.canGoBack()) {
            webView.goBack();
        } else {
            ActivityTaskManeger.getInstance().closeActivity(this);
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        webView.clearCache(true);
        webView.clearHistory();

    }

    public static void startWebAty(Context context, String url){
        Intent intent=new Intent(context,AutoWebActivity.class);
        intent.putExtra("url",url);
        context.startActivity(intent);
    }

    public static void startWebAty(Context context, String url,String title){
        Intent intent=new Intent(context,AutoWebActivity.class);
        intent.putExtra("url",url);
        intent.putExtra("title",title);
        context.startActivity(intent);
    }

    @Override
    public void onBackPressed() {
        if (webView.canGoBack()){
            webView.goBack();
        }else {
            ActivityTaskManeger.getInstance().closeActivity(mActivity);
        }
    }
}
